<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\admin;

use app\admin\controller\Admin;
use think\Db;
use app\common\builder\ZBuilder;
/**
 * 门店→仓库关联管理
 * @package app\cms\admin
 */
class StoreWarehouse extends Admin
{
    /**
     * 首页
     * @author  dou<51779987@qq.com>
     * @return mixed
     */
    public function index($store_id = [])
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        //数据接收
        $data_list = model('ErpStoreWarehouse', 'model') -> getStoreWarehouse($store_id);
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('关联仓库管理') // 设置页面标题
            ->setPrimaryKey('id') // 设置主键名为username
            ->setTableName('erp_store_warehouse') // 设置数据表名
            ->addColumns([ // 批量添加列
                ['wid', '仓库ID'],
                ['title', '仓库名称'],
                ['address', '仓库地址'],
                ['code', '仓库编码'],
                ['type', '仓库类型',['1'=>'分仓','2'=>'总仓']],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
            //->setSearch(['id' => '仓库ID', 'title' => '仓库名称', 'code' => '仓库编码']) // 设置搜索参数
            ->addTopButton('custom', ['title' => '新增', 'href' => url('erp/store_warehouse/add', ['store_id' => $store_id])],true)
            ->addRightButton('delete') // 批量添加右侧按钮
            ->setRowList($data_list['data']) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    /**
     * 添加
     * @author  dou<51779987@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add($store_id = [])
    {
        if ($this->request->isPost()) {
            //数据接收
            $data = $this->request->post('', null, 'trim');
            $store_id = isset($data['store_id']) ? $data['store_id'] : '';
            //总仓分仓类别--暂无用预留
            /*$warehouse_id_z = isset($data['warehouse_id_z']) ? $data['warehouse_id_z'] : '';
            $warehouse_id_f = isset($data['warehouse_id_f']) ? $data['warehouse_id_f'] : '';
            if($warehouse_id_f && $warehouse_id_z){
                $data['list_warehouse_fz'] = array_merge($warehouse_id_z, $warehouse_id_f);
                unset($data['warehouse_id_f'], $data['warehouse_id_z']);
            }elseif ($warehouse_id_f || $warehouse_id_z) {
                $data['list_warehouse_fz'] = $warehouse_id_f?$warehouse_id_f:$warehouse_id_z;
                if($warehouse_id_f){
                    unset($data['warehouse_id_f']);
                }
                if($warehouse_id_z){
                    unset($data['warehouse_id_z']);
                }
            } else{
                $data['list_warehouse_fz'] = '';
            }*/
            $warehouse_id = isset($data['warehouse_id_zf']) ? $data['warehouse_id_zf'] : '';
            //数据验证
            $Validate = validate('StoreWarehouse', 'validate');
            if( !$Validate -> scene('warehouse_add') -> check($data) ){
                $this->error($Validate -> getError());
            }

            if($warehouse_id){
                $s_w = [];
                foreach($warehouse_id as $key => $value) {
                    $s_w[$key]['warehouse_id'] = $value;
                    $s_w[$key]['store_id'] = $store_id;
                    $s_w[$key]['create_time'] = time();
                    $s_w[$key]['update_time'] = time();
                }
                //循环插入数据
                $rel = model('ErpStoreWarehouse','model') -> addDataAll($s_w);
            }
            if($rel['code'] == 200){
                $this->success('新增成功', '', '_parent_reload');
            }else{
                $this->error($rel['message']);
            }

        }

        //获取总仓列表
        $list_warehouse_zf = get_warehouse_type(1000,$store_id);
		$warehouse_zf = [];
		foreach($list_warehouse_zf as $key => $value){
			$k = $value['id'];
			$warehouse_zf[$k] = $value['title'];
		}
		
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增门店') // 设置页面标题
            ->addHidden('store_id',$store_id)
            ->addSelect('warehouse_id_zf', '选择仓库', '', $warehouse_zf, '', 'multiple')
            //->addSelect('warehouse_id_f', '选择仓库-总仓','',$list_warehouse_z,'','multiple')
            ->fetch();
    }


}